package cn.zmit.kuxi.holder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.activity.HisCenterActivity;
import cn.zmit.kuxi.activity.JoinDetailActivity;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.JoinRecordAll;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.interfaces.OnListItemSelectListenerTwo;

import com.xdroid.functions.holder.ViewHolderBase;

/** * @author chenxu @parameter * @return */
public class JoinRecordAllHolder extends ViewHolderBase<JoinRecordAll> {
	private TextView P_title, P_goodsName, P_noWin, P_winPhone, P_time;
	private ImageView P_goodsPic, D_goodsPic;
	private TextView D_title, D_goodsName, D_joinNeed, D_joinCount, D_my_join,
			tv_join_time;
	private ProgressBar progressBar;
	private LinearLayout llProot, llDRoot;
	private Context context;
	private static OnListItemSelectListenerTwo<JoinRecordAll> onlistItemSelect;
	private TextView tv_goods_allneed;

	@SuppressLint({ "CutPasteId", "InflateParams" })
	@Override
	public View createView(LayoutInflater layoutInflater) {
		context = layoutInflater.getContext();
		View view = layoutInflater.inflate(R.layout.item_join_all, null);
		llProot = (LinearLayout) view.findViewById(R.id.ll_PRoot);
		P_title = (TextView) view.findViewById(R.id.tv_title);
		P_goodsName = (TextView) view.findViewById(R.id.tv_goodsName);
		// P_noWin = (TextView) view.findViewById(R.id.tv_noWin);
		P_winPhone = (TextView) view.findViewById(R.id.tv_winPhone);
		P_goodsPic = (ImageView) view.findViewById(R.id.imag_goodsPic);
		P_time = (TextView) view.findViewById(R.id.tv_time);
		tv_goods_allneed = (TextView) view.findViewById(R.id.tv_goods_allneed);
		D_goodsPic = (ImageView) view.findViewById(R.id.imag_Pic);
		llDRoot = (LinearLayout) view.findViewById(R.id.llRoot);
		D_title = (TextView) view.findViewById(R.id.tv_goods_title);
		D_goodsName = (TextView) view.findViewById(R.id.tv_goods_detail);
		D_joinNeed = (TextView) view.findViewById(R.id.tv_goods_join);
		progressBar = (ProgressBar) view.findViewById(R.id.progressBar_goods);
		D_my_join = (TextView) view.findViewById(R.id.tv_join_num);
		tv_join_time = (TextView) view.findViewById(R.id.tv_join_time);
		return view;
	}

	@Override
	public void showData(int position, final JoinRecordAll itemData) {

		if (itemData.getA_type() == "1") {// 进行中的记录
			llProot.setVisibility(View.GONE);
			llDRoot.setVisibility(View.VISIBLE);
			if (itemData.getA_url() != null && itemData.getA_url().length() > 0) {
				ImageDisplayer.getInstance()
						.dispalyImageWithRadius(
								Url.IMAGE_PICTURES + itemData.getA_url(),
								D_goodsPic, 0);
			} else {
				D_goodsPic.setImageResource(R.drawable.ic_launcher);
			}
			D_title.setText("第" + itemData.getA_title() + "期");
			D_goodsName.setText(itemData.getA_goodsName());
			tv_goods_allneed.setText("总需人数：" + itemData.getA_oinNeed());
			D_joinNeed.setText("已参与：" + itemData.getA_joinCount());
			D_my_join.setText("本期参与：" + itemData.getA_myJoinCount() + "次");
			double all = Integer.parseInt(itemData.getA_oinNeed());
			double need = Integer.parseInt(itemData.getA_joinCount());
			double result = (need / all) * 100;
			progressBar.setMax(100);
			progressBar.setProgress((int) result);

			llDRoot.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							JoinDetailActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("gp_id", itemData.getA_gp_id());
					bundle.putString("title", itemData.getA_title());
					bundle.putString("goodsName", itemData.getA_goodsName());
					bundle.putString("joinNeed", itemData.getA_oinNeed());
					bundle.putString("joinCount", itemData.getA_joinCount());
					bundle.putString("my_join", itemData.getA_myJoinCount());
					bundle.putString("userId", itemData.getP_userId());
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			});

		} else if (itemData.getP_type() == "2") {
			int detime = itemData.getOldtime() - itemData.getNowtime();
			if (detime > 0) {
				llProot.setVisibility(View.VISIBLE);
				llDRoot.setVisibility(View.GONE);
				if (itemData.getP_Url() != null
						&& itemData.getP_Url().length() > 0) {
					ImageDisplayer.getInstance().dispalyImageWithRadius(
							Url.IMAGE_PICTURES + itemData.getP_Url(),
							P_goodsPic, 0);
				} else {
					P_goodsPic.setImageResource(R.drawable.ic_launcher);
				}
				P_title.setText("第" + itemData.getP_title() + "期");
				P_goodsName.setText(itemData.getP_name());

				// P_noWin.setVisibility(View.GONE);
				P_winPhone.setText("敬请期待...");
				P_time.setText("揭晓时间：" + "等待中...");
				llProot.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Toast.makeText(context, "即将揭晓，敬请期待！", 0).show();
					}
				});
			} else {
				llProot.setVisibility(View.VISIBLE);
				llDRoot.setVisibility(View.GONE);
				if (itemData.getP_Url() != null
						&& itemData.getP_Url().length() > 0) {
					ImageDisplayer.getInstance().dispalyImageWithRadius(
							Url.IMAGE_PICTURES + itemData.getP_Url(),
							P_goodsPic, 0);
				} else {
					P_goodsPic.setImageResource(R.drawable.ic_launcher);
				}
				P_title.setText("第" + itemData.getP_title() + "期");
				P_goodsName.setText(itemData.getP_name());
				tv_join_time.setText("本期参与:" + itemData.getJoincount() + "次");
				if (itemData.getP_userId().equals(itemData.getP_winner_id())) {
					// P_noWin.setVisibility(View.GONE);
					P_winPhone.setText("中奖用户:" + itemData.getP_winner_phone()
							+ "(幸运号" + itemData.getP_lucky_num() + ")");
				} else {
					// P_noWin.setVisibility(View.GONE);
					P_winPhone.setText("中奖用户:" + itemData.getP_winner_phone()
							+ "(幸运号" + itemData.getP_lucky_num() + ")");
				}
				P_time.setText("揭晓时间：" + itemData.getP_time());
				llProot.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// Intent intent = new Intent(context,
						// JoinDetailActivity.class);
						// Bundle bundle = new Bundle();
						// bundle.putString("gp_id", itemData.getP_gp_id());
						// bundle.putString("title", itemData.getP_title());
						// bundle.putString("goodsName",
						// itemData.getA_goodsName());
						// bundle.putString("joinNeed",
						// itemData.getA_oinNeed());
						// bundle.putString("joinCount",
						// itemData.getA_joinCount());
						// bundle.putString("my_join",
						// itemData.getA_myJoinCount());
						// bundle.putString("userId", itemData.getP_userId());
						// intent.putExtras(bundle);
						// context.startActivity(intent);

						Intent intent = new Intent(context,
								JoinDetailActivity.class);
						Bundle bundle = new Bundle();
						bundle.putString("gp_id", itemData.getP_gp_id());
						bundle.putString("win_id", itemData.getP_winner_id());
						intent.putExtras(bundle);
						context.startActivity(intent);
					}
				});

			}
		}

	}

	// 设置回调
	public static void setOnListItemSelectListener(
			OnListItemSelectListenerTwo<JoinRecordAll> onListItemSelectListener) {
		JoinRecordAllHolder.onlistItemSelect = onListItemSelectListener;
	}
}
